/*     */ package com.zimbra.cs.mime.handler;
/*     */ 
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.io.Reader;
/*     */ import java.io.StringReader;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TextEnrichedHandler
/*     */   extends TextHtmlHandler
/*     */ {
/*     */   protected Reader getReader(InputStream is, String ctype)
/*     */     throws IOException
/*     */   {
/*  34 */     String content = Mime.decodeText(is, ctype, getDefaultCharset());
/*  35 */     return new StringReader(convertToHTML(content));
/*     */   }
/*     */   
/*  38 */   private static Map<String, String> sConversions = new HashMap();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static Map<String, String> sColors;
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String convertToHTML(String content)
/*     */   {
/*  58 */     if (content == null) {
/*  59 */       return null;
/*     */     }
/*  61 */     StringBuilder sb = new StringBuilder(content.length());
/*  62 */     int last = 0;int nofill = 0;
/*  63 */     int pos = 0; for (int length = content.length(); pos < length; pos++) { int c;
/*  64 */       switch (c = content.charAt(pos)) {
/*     */       case '\r': 
/*     */         break; case '\n':  sb.append((nofill > 0) || (last == c) ? "<br/>\n" : " "); break;
/*  67 */       case ' ':  if ((last == c) || ((last != 160) && (pos != length - 1) && (content.charAt(pos + 1) == ' '))) {
/*  68 */           sb.append("&nbsp;");c = 160;
/*     */         }
/*     */         break;
/*  71 */       default:  sb.append((char)c); break;
/*  72 */       case '&':  sb.append("&amp;"); break;
/*  73 */       case '>':  sb.append("&gt;"); break;
/*     */       }
/*  75 */       int gt = content.indexOf('>', pos);
/*  76 */       if ((gt == -1) || (gt - pos > 61)) {
/*  77 */         sb.append("&lt;");
/*  78 */       } else if (content.charAt(pos + 1) == '<') {
/*  79 */         sb.append("&lt;");pos++;
/*     */       } else {
/*  81 */         String format = content.substring(pos + 1, gt).toLowerCase();
/*  82 */         String tag = (String)sConversions.get(format);
/*  83 */         if (format.equals("nofill")) {
/*  84 */           nofill++;
/*  85 */         } else if (format.equals("/nofill")) {
/*  86 */           nofill--;nofill = Math.max(nofill, 0);
/*  87 */         } else if (tag != null) {
/*  88 */           sb.append(tag);
/*  89 */         } else if (format.equals("param")) {
/*  90 */           int end = content.indexOf("</param>", gt);
/*  91 */           if (end != -1)
/*  92 */             gt = end + 7;
/*  93 */         } else if (format.equals("color")) {
/*  94 */           sb.append("<font color=#").append(readColorParam(content, gt + 1)).append(">");
/*  95 */         } else if (format.equals("fontfamily")) {
/*  96 */           String family = readParam(content, gt + 1);
/*  97 */           sb.append("<font face=\"").append(family == null ? "Times" : encodeParam(family)).append("\">");
/*     */         }
/*  99 */         pos = gt;
/*     */       }
/* 101 */       last = c;
/*     */     }
/* 103 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private static String readParam(String content, int pos) {
/* 107 */     int end = content.indexOf("</param>", pos);
/* 108 */     if ((content.startsWith("<param>", pos)) && (end != -1))
/* 109 */       return content.substring(pos + 7, end);
/* 110 */     return null;
/*     */   }
/*     */   
/*     */   private static String encodeParam(String param) {
/* 114 */     StringBuilder sb = new StringBuilder(param.length());
/* 115 */     int i = 0; for (int length = param.length(); i < length; i++) {
/* 116 */       int c = param.charAt(i);
/* 117 */       switch (c) {
/*     */       case 10: case 13: 
/*     */         break;
/* 120 */       case 60:  sb.append("&lt;"); break;
/* 121 */       case 62:  sb.append("&gt;"); break;
/* 122 */       case 38:  sb.append("&amp;"); break;
/* 123 */       case 34:  sb.append("&quot;"); break;
/* 124 */       default:  sb.append((char)c);
/*     */       }
/*     */     }
/* 127 */     return sb.toString();
/*     */   }
/*     */   
/*     */   static
/*     */   {
/*  40 */     sConversions.put("bold", "<b>");sConversions.put("/bold", "</b>");
/*  41 */     sConversions.put("italic", "<i>");sConversions.put("/italic", "</i>");
/*  42 */     sConversions.put("underline", "<u>");sConversions.put("/underline", "</u>");
/*  43 */     sConversions.put("fixed", "<tt>");sConversions.put("/fixed", "</tt>");
/*  44 */     sConversions.put("excerpt", "<q>");sConversions.put("/excerpt", "</q>");
/*     */     
/*  46 */     sConversions.put("smaller", "<font size=-1>");sConversions.put("/smaller", "</font>");
/*  47 */     sConversions.put("bigger", "<font size=+1>");sConversions.put("/bigger", "</font>");
/*  48 */     sConversions.put("center", "<div align=center>");sConversions.put("/center", "</div>");
/*  49 */     sConversions.put("flushleft", "<div align=left>");sConversions.put("/flushleft", "</div>");
/*  50 */     sConversions.put("flushright", "<div align=right>");sConversions.put("/flushright", "</div>");
/*  51 */     sConversions.put("flushboth", "<div align=justify>");sConversions.put("/flushboth", "</div>");
/*     */     
/*  53 */     sConversions.put("/color", "</font>");
/*  54 */     sConversions.put("/fontfamily", "</font>");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 130 */     sColors = new HashMap();
/*     */     
/* 132 */     sColors.put("red", "FF0000");sColors.put("blue", "0000FF");
/* 133 */     sColors.put("green", "008000");sColors.put("yellow", "FFFF00");
/* 134 */     sColors.put("cyan", "00FFFF");sColors.put("magenta", "FF00FF");
/* 135 */     sColors.put("black", "000000");sColors.put("white", "FFFFFF");
/*     */   }
/*     */   
/* 138 */   private static Pattern RGB_COLOR_PATTERN = Pattern.compile("(\\p{XDigit}{2})\\p{XDigit}{2},(\\p{XDigit}{2})\\p{XDigit}{2},(\\p{XDigit}{2})\\p{XDigit}{2}");
/*     */   
/*     */   private static String readColorParam(String content, int pos) {
/* 141 */     String color = readParam(content, pos);
/* 142 */     if (color != null) {
/* 143 */       String triplet = (String)sColors.get(color);
/* 144 */       if (triplet != null)
/* 145 */         return triplet;
/* 146 */       Matcher matcher = RGB_COLOR_PATTERN.matcher(color);
/* 147 */       if (matcher.matches()) {
/* 148 */         return (matcher.group(1) + matcher.group(2) + matcher.group(3)).toUpperCase();
/*     */       }
/*     */     }
/* 151 */     return "000000";
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 155 */     String test = "<bold>Now</bold> is the time for <italic>all</italic>\ngood men\n<smaller>(and <<women>)</smaller> to\n<ignoreme>come</ignoreme>\n\nto the aid of their\n\n\n<color><param>red</param>beloved</color>\ncountry.\n\nBy the way,\nI think that <paraindent><param>left</param><<smaller>\n</paraindent>should REALLY be called\n\n<paraindent><param>left</param><<tinier></paraindent>\nand that I am always right.\n\n<center>-- the end</center>\n";
/*     */     
/*     */ 
/*     */ 
/* 159 */     System.out.println(convertToHTML(test));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/handler/TextEnrichedHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */